package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.models.Shop;
import com.yunchang.springboot.models.mwsreportbean.MwsReimbursements;
import com.yunchang.springboot.models.mwsreportbean.MwsReimbursementsDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28  18:43
 * @description:
 */
@Component
public class ReimbursementsDataDispose extends ReportDataDisposeParent implements IReportDataDispose {
    /**
     * 赔偿 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        int approvalDateIndex = Integer.valueOf(String.valueOf(titleMap.get("approval-date")));
        int reimbursementIdIndex = Integer.valueOf(String.valueOf(titleMap.get("reimbursement-id")));
        int caseIdIndex = Integer.valueOf(String.valueOf(titleMap.get("case-id")));
        int amazonOrderIdIndex = Integer.valueOf(String.valueOf(titleMap.get("amazon-order-id")));
        int reasonIndex = Integer.valueOf(String.valueOf(titleMap.get("reason")));
        int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
        int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
        int asinIndex = Integer.valueOf(String.valueOf(titleMap.get("asin")));
        int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("product-name")));
        int conditionIndex = Integer.valueOf(String.valueOf(titleMap.get("condition")));
        int currencyUnitIndex = Integer.valueOf(String.valueOf(titleMap.get("currency-unit")));
        int amountPerUnitIndex = Integer.valueOf(String.valueOf(titleMap.get("amount-per-unit")));
        int amountTotalIndex = Integer.valueOf(String.valueOf(titleMap.get("amount-total")));
        int quantityReimbursedCashIndex = Integer.valueOf(String.valueOf(titleMap.get("quantity-reimbursed-cash")));
        int quantityReimbursedInventoryIndex = Integer.valueOf(String.valueOf(titleMap.get("quantity-reimbursed-inventory")));
        int quantityReimbursedTotalIndex = Integer.valueOf(String.valueOf(titleMap.get("quantity-reimbursed-total")));
        int originalReimbursementIdIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("original-reimbursement-id")) {
            originalReimbursementIdIndex = Integer.valueOf(String.valueOf(titleMap.get("original-reimbursement-id")));
        }
        int originalReimbursementTypeIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("original-reimbursement-type")) {
            originalReimbursementTypeIndex = Integer.valueOf(String.valueOf(titleMap.get("original-reimbursement-type")));
        }
        List<Object> insertList = new ArrayList<>(lists.size());
        for (int i = 1; i < lists.size(); i++) {
            MwsReimbursementsDO mwsReimbursements = new MwsReimbursementsDO();
            List<String> row = lists.get(i);
            mwsReimbursements.setSellerId(sellerId);
            mwsReimbursements.setArea(area);
            mwsReimbursements.setApprovalDate(DateUtil.ISO8601ToGMT(row.get(approvalDateIndex)));
            mwsReimbursements.setReimbursementId(row.get(reimbursementIdIndex));
            mwsReimbursements.setCaseId(row.get(caseIdIndex));
            mwsReimbursements.setAmazonOrderId(row.get(amazonOrderIdIndex));
            mwsReimbursements.setReason(row.get(reasonIndex));
            mwsReimbursements.setSku(row.get(skuIndex));
            mwsReimbursements.setFnsku(row.get(fnskuIndex));
            mwsReimbursements.setAsin(row.get(asinIndex));
            mwsReimbursements.setProductName(row.get(productNameIndex));
            mwsReimbursements.setGoodsCondition(row.get(conditionIndex));
            mwsReimbursements.setCurrencyUnit(row.get(currencyUnitIndex));
            mwsReimbursements.setAmountPerUnit(new BigDecimal(String.valueOf(row.get(amountPerUnitIndex))));
            mwsReimbursements.setAmountTotal(new BigDecimal(String.valueOf(row.get(amountTotalIndex))));
            mwsReimbursements.setQuantityReimbursedCash(ToolUtil.valueOfInteger(row.get(quantityReimbursedCashIndex)));
            mwsReimbursements.setQuantityReimbursedInventory(ToolUtil.valueOfInteger(row.get(quantityReimbursedInventoryIndex)));
            mwsReimbursements.setQuantityReimbursedTotal(ToolUtil.valueOfInteger(row.get(quantityReimbursedTotalIndex)));
            if (originalReimbursementIdIndex != WRONG_NUMBER_SIGN) {
                mwsReimbursements.setOriginalReimbursementId(row.get(originalReimbursementIdIndex));
            }
            if (originalReimbursementTypeIndex != WRONG_NUMBER_SIGN) {
                mwsReimbursements.setOriginalReimbursementType(row.get(originalReimbursementTypeIndex));
            }
            mwsReimbursements.setCreateTime(DateUtil.getDate());
            mwsReimbursements.setUpdateTime(DateUtil.getDate());
            mwsReimbursements.setFinanceStatus(1);
            insertList.add(mwsReimbursements);
        }
        return insertList;
    }
}
